package net.cl.support.codec;

import java.security.MessageDigest;

import net.cl.utils.ByteUtil;

/**
 * Secure Hash Algorithm 安全散列算法
 * 
 * @author caol64@gmail.com
 */
public class SHA1 {

	/**
	 * SHA1校验(40位)
	 */
	public static String encode(byte[] in) throws CodecException {
		if (in == null)
			return null;
		try {
			MessageDigest sha1 = MessageDigest.getInstance("SHA");
			sha1.update(in);
			byte[] b = sha1.digest();
			return ByteUtil.bytes2HexString(b);
		} catch (Exception e) {
			throw new CodecException(e);
		}
	}

	/**
	 * SHA1校验(40位)
	 */
	public static String encode(String in) throws CodecException {
		if (in == null)
			return null;
		return encode(in.getBytes());
	}

}
